﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
namespace QuanLyThi
{
    public partial class FrmCauHinhKhoaDaoTao : Form
    {
        public static string mConnect = FrmMain.mConnect;
        private string msql = "";
        public DGC_Library mLibrary = new DGC_Library(mConnect);

        public FrmCauHinhKhoaDaoTao()
        {
            InitializeComponent();
            this.Load();
            comboBox1_SelectionChangeCommitted(null, null);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Load()
        {
            msql = "Select * from tb_khoadaotao order by Khoadaotao";
            DataTable dt = mLibrary.GetDataTable(msql, "table");
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "khoadaotao";
            comboBox1.ValueMember = "MS_khoadaotao";

        }      

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
           
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //Danh sach khoa da cau hinh
            string ma = comboBox1.SelectedValue.ToString();
            msql = "Select a.ms_Mondaotao,a.mondaotao,a.ghichu "+
                   " from tb_Mondaotao a inner join tb_CauhinhKhoa b on a.MS_Mondaotao=b.MS_Mondaotao "+
                   " where B.ms_Khoadaotao=" + ma + " Order by Mondaotao";
            DataTable dt1 = mLibrary.GetDataTable(msql, "tb1");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt1;
            //Danh sach khoa chưa cau hinh
            msql = "Select * from TB_mondaotao where MS_Mondaotao not in ( Select MS_Mondaotao " +
                   "from tb_CauhinhKhoa where ms_khoadaotao=" + ma + " ) Order by Mondaotao ";
            DataTable dt2 = mLibrary.GetDataTable(msql, "tb1");
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = dt2;
        }

        private void butup_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView2.RowCount > 0)
                {
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView1.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView2.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView2.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView2.CurrentRow.Cells[2].Value.ToString();
                    dt.Rows.Add(newrow);
                    //Delete row phia trên
                    DataGridViewRow row = dataGridView2.CurrentRow;
                    dataGridView2.Rows.Remove(row);
                    //DataTable dt2 = (DataTable)dataGridView2.DataSource;
                    //dt2.Rows[dataGridView2.CurrentRow.Index].Delete();

                 
                    //dataGridView2.DataSource = dt2;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void butdown_Click(object sender, EventArgs e)
        {
            try
            {                
                if (this.dataGridView1.RowCount > 0)
                {                   
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView2.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                    dt.Rows.Add(newrow);
                    //Delete row phia trên
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                    //DataTable dt2 = (DataTable)dataGridView1.DataSource;
                    //dt2.Rows[dataGridView1.CurrentRow.Index].Delete();
                    //dataGridView1.DataSource = dt2;
                }                
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void butLuu_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialog = MessageBox.Show("Bạn có muốn lưu thông tin này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes)
                {
                    string maCTDT = comboBox1.SelectedValue.ToString();
                    //Xóa thông tin củ
                    msql = "Delete tb_CauhinhKhoa Where ms_Khoadaotao=" + maCTDT;
                    mLibrary.RunProc(msql);
                    //cập nhật thông tin mới
                    string MaKhoaDT = LayMaKhoa();
                    msql = "  Insert into tb_CauhinhKhoa Select " + maCTDT + " as MS_KhoaDaotao,MS_Mondaotao,getdate() from tb_Mondaotao where MS_Mondaotao in (" + MaKhoaDT + " )";
                    mLibrary.RunProc(msql);
                    MessageBox.Show("Lưu thông tin thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }   
            }
            catch (Exception)
            {
                MessageBox.Show("Lưu thông tin không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }           
        }
        private string LayMaKhoa()
        {
            string re = "0";
            DataTable dt=(DataTable)dataGridView1.DataSource;
            if(dt.Rows.Count>0)
            {
                for (int i = 0; i <= dt.Rows.Count-1; i++)
                {
                    string Ma = "0";
                    if (dt.Rows[i]["MS_monDaoTao"].ToString() == null)
                        Ma = "0";
                    else
                        Ma = dt.Rows[i]["MS_monDaoTao"].ToString();
                    re = re + "," + Ma;
                }
            }
            return re;
        }
     
    }
}
